//--------------------------------------- // Send a packet to the server //--------------------------------------- public bool SendPacket(OldPacket pkt) { if (!m_sendLock.WaitOne(Constants.ResponseTimeout)) //do one send at a time { return(false); } byte[] toSend; OnSendData(pkt, out toSend); DataChunk wrap = new DataChunk(toSend.Length) { Data = toSend }; try { m_sock.BeginSend(wrap.Data, 0, wrap.Data.Length, SocketFlags.None, _sendCB, wrap); } catch (SocketException) { //connection aborted by hardware errors produce a socketexception. return(false); } return(true); }
public void InvokeHandler(OldPacket pkt, bool isInGame) { //force ignore if the handler is an in-game only handler if (_inGameOnly && !isInGame) { return; } _handler(pkt); }
//protected override void OnSendData(OldPacket pkt, out byte[] toSend) //{ // //for debugging: sometimes the server is getting PACKET_INTERNAL from this client // //I'm not sure why and it happens randomly so this check will allow me to examine // // packet contents and the call stack to figure it out // if (pkt.Family == PacketFamily.Internal) // throw new ArgumentException("This is an invalid packet!"); // toSend = _packetProcessActions.EncodePacket(pkt); // //at this point, toSend should be 3 or 4 bytes longer than the original packet data // //this includes 2 bytes of len, 1 or 2 bytes of seq, and then packet payload // if (EventSendData != null) // { // DataTransferEventArgs dte = new DataTransferEventArgs(DataTransferEventArgs.TransferType.Send, pkt.Family, pkt.Action, pkt.Data); // EventSendData(dte); // } //} protected override void OnSendRawData(OldPacket pkt, out byte[] toSend) { if (EventSendData != null) { DataTransferEventArgs dte = new DataTransferEventArgs(DataTransferEventArgs.TransferType.SendRaw, pkt.Family, pkt.Action, pkt.Data); EventSendData(dte); } pkt.WritePos = 0; pkt.AddShort((short)pkt.Length); toSend = pkt.Get(); }
private void _handlePacket(object state) { OldPacket pkt = (OldPacket)state; FamilyActionPair pair = new FamilyActionPair(pkt.Family, pkt.Action); bool handled = false; if (_handlers.ContainsKey(pair)) { handled = true; _handlers[pair].InvokeHandler(pkt, IsInGame); } if (EventReceiveData != null) { DataTransferEventArgs dte = new DataTransferEventArgs(DataTransferEventArgs.TransferType.Recv, pkt.Family, pkt.Action, pkt.Data) { PacketHandled = handled }; EventReceiveData(dte); } }
/// <summary> /// Send unencrypted packet to server /// </summary> public bool SendRaw(OldPacket pkt) { m_sendLock.WaitOne(); byte[] toSend; OnSendRawData(pkt, out toSend); DataChunk wrap = new DataChunk(toSend.Length) { Data = toSend }; try { m_sock.BeginSend(wrap.Data, 0, wrap.Data.Length, SocketFlags.None, _sendCB, wrap); } catch (SocketException) { return(false); } return(true); }
protected override void OnReceiveData(DataChunk state) { EODataChunk wrap = (EODataChunk)state; if (wrap.Data.Length == 0) { wrap.State = EODataChunk.DataReceiveState.NoData; } try { switch (wrap.State) { case EODataChunk.DataReceiveState.ReadLen1: { wrap.RawLength[0] = wrap.Data[0]; wrap.State = EODataChunk.DataReceiveState.ReadLen2; wrap.Data = new byte[EODataChunk.BUFFER_SIZE]; StartDataReceive(wrap); break; } case EODataChunk.DataReceiveState.ReadLen2: { wrap.RawLength[1] = wrap.Data[0]; wrap.State = EODataChunk.DataReceiveState.ReadData; wrap.Data = new byte[OldPacket.DecodeNumber(wrap.RawLength)]; StartDataReceive(wrap); break; } case EODataChunk.DataReceiveState.ReadData: { //var pkt = _packetProcessActions.DecodeData(wrap.Data); ////This block handles receipt of file data that is transferred to the client. ////It should make file transfer nuances pretty transparent to the client. ////The header for files stored in a Packet type is always as follows: FAMILY_INIT, ACTION_INIT, (InitReply) ////A 3-byte offset is found throughout the code that handles creating these files. ////INIT_INIT packet! check to see if expecting a file or player list //if (pkt.Family == PacketFamily.Init && pkt.Action == PacketAction.Init) //{ // byte[] data = pkt.Get(); // byte reply = pkt.GetChar(); // if (ExpectingFile || reply == (byte)InitReply.INIT_MAP_MUTATION) //handle the map mutation: should work with the byte/char weirdness // { // int dataGrabbed = 0; // //find first zero byte // int pktOffset = 0; // for (; pktOffset < data.Length; ++pktOffset) // if (data[pktOffset] == 0) // break; // //continue receiving until we have grabbed enough data to fill the allocated packet buffer // do // { // byte[] fileBuffer = new byte[pkt.Length - pktOffset]; // int nextGrabbed = ReceiveRaw(ref fileBuffer); // Array.Copy(fileBuffer, 0, data, dataGrabbed + 3, data.Length - (dataGrabbed + pktOffset)); // dataGrabbed += nextGrabbed; // } while (dataGrabbed < pkt.Length - pktOffset); // if (pktOffset > 3) // data = data.SubArray(0, pkt.Length - (pktOffset - 3)); // //rewrite the InitReply with the correct value (retrieved with GetChar, server sends with GetByte for other reply types) // data[2] = reply; // } // else if (ExpectingPlayerList) // { // //online list sends a char... rewrite it with a byte so it is parsed correctly. // data[2] = reply; // } // pkt = new OldPacket(data); //} //_handlePacket(pkt); //var newWrap = new EODataChunk(); //StartDataReceive(newWrap); break; } default: { Console.WriteLine("ERROR: Invalid data wrapper state in _recvCB (should be ReadLen1, ReadLen2, or ReadData). Closing connection."); Disconnect(); break; } } } catch (SocketException se) { //in the process of disconnecting Console.WriteLine("There was a SocketException with SocketErrorCode {0} in _recvCB", se.SocketErrorCode); } }
/// <summary> /// Does optional processing of raw packet data before sending it to the server /// </summary> protected virtual void OnSendRawData(OldPacket pkt, out byte[] toSend) { toSend = pkt.Get(); }