Register for your free account! | Forgot your password?

Go Back   elitepvpers > Popular Games > Silkroad Online > SRO Coding Corner
You last visited: Today at 15:11

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[HELP] One question about programing

Discussion on [HELP] One question about programing within the SRO Coding Corner forum part of the Silkroad Online category.

Reply
 
Old 03/07/2011, 00:57   #31
 
npcdoom's Avatar
 
elite*gold: 0
Join Date: Jun 2009
Posts: 76
Received Thanks: 147
Well for send, you always have to copy the value of the data you wanna send into the buffer. Your way is fine, but i kinda prefer keeping a pointer to the current position.

Code:
 *(int*)ptrPos = 12;
Yours is access element n,get address and then dereference, mine is just deference also cleaner to read, but in overall is the same.
npcdoom is offline  
Thanks
1 User
Old 03/07/2011, 01:06   #32
 
kevin_owner's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 1,484
Received Thanks: 809
Quote:
Originally Posted by npcdoom View Post
Well for send, you always have to copy the value of the data you wanna send into the buffer. Your way is fine, but i kinda prefer keeping a pointer to the current position.

Code:
 *(int*)ptrPos = 12;
Yours is access element n,get address and then dereference, mine is just deference also cleaner to read, but in overall is the same.
Thanks indeed it's a bit clearer but how do you add the packet size at the beginning?

in my way I create a placeholder at the beginning of my buffer and when the packet is ready to send is store the current position into another variable set the current position at 0 en add the size which i saved in that other variable.
kevin_owner is offline  
Old 03/07/2011, 01:12   #33
 
elite*gold: 0
Join Date: Nov 2008
Posts: 31
Received Thanks: 29
Quote:
Originally Posted by kevin_owner View Post
actually I want to copy the value into that array cause that's the way I build the packet. the reason i'm using this is cause i didn't find another way yet cause this one works fine for me.

Look my packet writer has a member which is currentPosition which i'm using for the index in that char array. so when I add a value lets say a short but it could also be a int I increase the currentPosition with the size of an int or short so it doesn't overwrite some data.

I'm sure that there will be an easier way and that it'll confuse some people and i have to admit that i went way to far in the code. Sorry for that:P

but would you mind telling me how you would build a packet to send cause that's the only way I know and i'd like to know more/better ways?
Yes, if you actually want to copy the data, then you have to do it the way you are doing it, more or less. Given the code of yours, I would however suggest few slight modifications, just to polish it a bit. As I have (not) mentioned above, using brackets might be a bit slower than doing the incrementation of pointer; perhaps like this...

Code:
*(int*)(buffer + position) = value;
However, my approach to this issue would be very different...consider this way of doing it.

Code:
/* i will take an assumption of the buffer being a lovely pointer monster... */

struct packet_buffer {
  unsigned char *beg;
  unsigned char *end;
  unsigned char *cur;
};


/* and lets suppose we have some nice function that tries to append the
   given value to the buffer. if that fails, NULL is returned... */

int packet_buffer_append(void *value, size_t len, struct packet_buffer *buf) {
  if (buf->cur + len > buf->end)
    return 0;

  /* we could make use of the <string.h> function called <memcpy> which
     would do the dirty work of copying data over;

  memcpy(buf->cur, value, len);

  /* increment the current position by <len> bytes of the data copied */

  buf->cur += len;

  /* just return something; why not pointer to current position in buffer? */
  return buf->cur;
}
Anyway, maybe that kind of way is too C-ish, not modern enough nowadays, with all that C++ whatever rush.

Also, you should know that compilers have this nasty behaviour of aligning structures to fit certain number of bytes (well, usually to be multiple of the system atomic size - e.g multiple of 4 or 8 bytes).

This means that if you have struct packet { short length, opcode, secure; char element; } it wont have 7 bytes but rather 8 bytes, with one byte stuck somewhere...
dracek is offline  
Thanks
2 Users
Old 03/07/2011, 10:27   #34
 
kevin_owner's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 1,484
Received Thanks: 809
Thanks that seems to be another good way to packet the data I'll run a little test to see which one is the fastest cause I'm curious about that part

about that struct I didn't know that I've never just such kind of struct to build my packet.

Thanks
kevin_owner is offline  
Old 03/07/2011, 16:35   #35
 
elite*gold: 0
Join Date: Nov 2008
Posts: 31
Received Thanks: 29
Quote:
Originally Posted by kevin_owner View Post
Thanks that seems to be another good way to packet the data I'll run a little test to see which one is the fastest cause I'm curious about that part

about that struct I didn't know that I've never just such kind of struct to build my packet.

Thanks
Well, it can be real issue not only in the packet related logic but also in raw data interpretation - imagine having a collision system that has to read meshes of the present 3d objects into memory in order to compute possible intersections.

The mesh has its very specific format and if you had, for example, structure that would represent single pack of vertices and used code like following one, you would find out that the data read are invalid, wrong.

Code:
struct object3d_mesh_node { float x, y, z; char flag; };
/* <real> size is 3*4 + 1 = 13 bytes; these will be aligned to fit 16 bytes */

object3d_mesh_node node;
for (i = 0; i < count; ++i) {
  fread(&node, sizeof(node), 1, fp);
}

/* now, you read 16 bytes instead of 13 bytes
   ... guess what happens? BAM! */
dracek is offline  
Old 03/07/2011, 18:05   #36
 
kevin_owner's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 1,484
Received Thanks: 809
Quote:
Originally Posted by dracek View Post
Well, it can be real issue not only in the packet related logic but also in raw data interpretation - imagine having a collision system that has to read meshes of the present 3d objects into memory in order to compute possible intersections.

The mesh has its very specific format and if you had, for example, structure that would represent single pack of vertices and used code like following one, you would find out that the data read are invalid, wrong.

Code:
struct object3d_mesh_node { float x, y, z; char flag; };
/* <real> size is 3*4 + 1 = 13 bytes; these will be aligned to fit 16 bytes */

object3d_mesh_node node;
for (i = 0; i < count; ++i) {
  fread(&node, sizeof(node), 1, fp);
}

/* now, you read 16 bytes instead of 13 bytes
   ... guess what happens? BAM! */
yeah you're right but wasn't there an easy way to solve it?
something like:

Code:
#pragma pack(push, 1)
Edit: I just tested the pragma pack line and it does work. without that line of code the sizeof that struct would be 16 just like you said and with that line of code it's 13.
kevin_owner is offline  
Old 03/07/2011, 23:32   #37
 
elite*gold: 0
Join Date: Nov 2008
Posts: 31
Received Thanks: 29
Quote:
Originally Posted by kevin_owner View Post
yeah you're right but wasn't there an easy way to solve it?
something like:

Code:
#pragma pack(push, 1)
Edit: I just tested the pragma pack line and it does work. without that line of code the sizeof that struct would be 16 just like you said and with that line of code it's 13.
Yes, that does the trick. The #pragma pack directive is also in gcc (for msvc compatibility) - which is quite great; eases porting of code.
dracek is offline  
Old 03/11/2011, 08:05   #38
 
lesderid's Avatar
 
elite*gold: 0
Join Date: Dec 2007
Posts: 2,400
Received Thanks: 1,517
Quote:
Originally Posted by npcdoom View Post
Speed is always an important issue, not language, just because pc are getting really fast, you cant just do whatever you want =P, but it all depends on the requirements and the problem you trying to solve.
I was just saying that when you are measuring code performance, you'll see that how you code something is (often) more important than in what language you code it.

But indeed, when coding an emulator, you have to make sure that stuff isn't too slow. (but I think you know that...)

Edit: @dracek: Maybe something stupid but aren't you doing the "buf->cur + len" calculation twice? Or would the compiler optimize this?
Quote:
Originally Posted by dracek
Code:
/* i will take an assumption of the buffer being a lovely pointer monster... */

struct packet_buffer {
  unsigned char *beg;
  unsigned char *end;
  unsigned char *cur;
};


/* and lets suppose we have some nice function that tries to append the
   given value to the buffer. if that fails, NULL is returned... */

int packet_buffer_append(void *value, size_t len, struct packet_buffer *buf) {
  size_t endLength = buf->cur + len;
  //if (buf->cur + len > buf->end)
  if (endLength > buf->end)
    return 0;

  /* we could make use of the <string.h> function called <memcpy> which
     would do the dirty work of copying data over;

  memcpy(buf->cur, value, len);

  /* increment the current position by <len> bytes of the data copied */

  //buf->cur += len;
  buf->cur = endLength;

  /* just return something; why not pointer to current position in buffer? */
  return buf->cur;
}
lesderid is offline  
Reply


Similar Threads Similar Threads
[Programing] what is the best programing launguage
10/31/2010 - CO2 Programming - 19 Replies
hi i am willing to learn a new programming launguage to create bots witch i will post for free useing so if you can please tell me what is the best one to learn i will start learning it as soon as possible i allready know a bit about VB
[HELP]how to start programing ?
06/28/2009 - CO2 Programming - 30 Replies
i just finished my final exams today if i want to start learning programing .. what u should i start with ,, i mean what language to start where is the best to learn it ., if any one wants to help just say it
new to programing
12/20/2008 - Cabal Hacks, Bots, Cheats, Exploits & Macros - 0 Replies
first of all, i want to thank you all for the help you got me from other threads. now i would like to ask something... no i don't want bots or hacks made from anyone... i just want a little advice in something. to know about programing a bypass for gameguard or to make a bot which programs, coder or programing books i should look to?? i mean, should i look for auto it to make a bot? should i try c++ programing? if theres someone who could help me with knowing more about programing...
Help for Programing a bot
04/01/2008 - Conquer Online 2 - 0 Replies
Well, i want to make a bot for CO, but i need some help. I don't know if it's better to take the packets that send conquer server, and manipulate it; or find the memory address and change the values when i need it. Well, in addition, i have a question. Always the system assigned the same memory address for the game? For example, if i use the "odbg", and i find that the memory address for the player name is "x", always "x" have the player name? Well, i need some help. I don't need that...
Request for programing help
05/30/2006 - Conquer Online 2 - 1 Replies
Well ass all of we know the auto lvl doent work i use to lvl whit COPartner but now its freez. Ill try to crack it but i need some help hehe if anyone can give me and idea of how to do it or any source ty hope elitepvpers ll join togetter to do this crack lol by the way im not good programer so i need so much help



All times are GMT +1. The time now is 15:11.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.