public int Read(Byte[] Data) { ReadSize = 0; SendSocketDataAvailableNotification = false; if (IsConnected && (_SocketClient.Available > 0)) { try { #if ASYNC_RECEIVE ReceiveDone.Reset(); int ByteToRead = Math.Min(Data.Length, _SocketClient.Available); _SocketClient.BeginReceive(Data, 0, ByteToRead, SocketFlags.None, new AsyncCallback(ReadCallBack), _SocketClient); ReceiveDone.WaitOne(); #else ReadSize = _SocketClient.Receive(Data); #endif } #if DEBUG_SOCKET catch (Exception e) { AccessoryLibrary.ShowException(e); ReceiveDone.Set(); ReadSize = -1; } #else catch (Exception ex) { ReceiveDone.Set(); ReadSize = -1; logger.Error(ex.ToString() + "\n"); } #endif } /* * in the calling procedure * * After calling Read() * ResetDataAvailableNotification() must be called again * to enable data notification * */ return(ReadSize); }
private void OnReceive(IAsyncResult ar) { if (!IsAlive) { return; } using (ByteBuffer buffer = (ByteBuffer)ar.AsyncState) { buffer.Position = 0; try { buffer.Limit = Socket.EndReceive(ar); if (buffer.Limit == 0) { Stop(); } else { using (Packet inPacket = new Packet(Cryptograph.Decrypt(buffer.GetContent()))) { // parse information from packet short packetOPCODE = inPacket.OperationCode; string packetName = Enum.GetName(typeof(TReceiveOP), packetOPCODE); byte[] packetByteArray = inPacket.Array; string currentTime = DateTime.Now.ToString("HH:mm:ss"); if (Enum.IsDefined(typeof(TReceiveOP), inPacket.OperationCode)) { switch (Packet.LogLevel) { case LogLevel.Name: Log.Inform("[{0}][ServerHandler]: \n Received [{1}] packet.", currentTime, packetName); break; case LogLevel.Full: Log.Hex("Received {0} packet: ", packetByteArray, packetName); break; case LogLevel.None: break; default: throw new ArgumentOutOfRangeException(); } } else { Log.SkipLine(); Log.Hex("Received unknown (0x{0:X2}) packet: ", packetByteArray, packetOPCODE); Log.SkipLine(); } Dispatch(inPacket); } } ReceiveDone.Set(); } catch (Exception e) { Log.SkipLine(); Log.Error("Uncatched fatal error on {0}: ", e, Title.ToLower()); Stop(); } } }
private void OnReceive(IAsyncResult ar) { if (!IsAlive) { return; } try { int received = Socket.EndReceive(ar); if (received != 0) { ReceivalBuffer.Limit += received; if (Cryptograph is MapleCryptograph) { int processed = 0; bool reset = false; if (ReceivalBuffer.Remaining < 4) { Log.Error("TODO: Remaining < 4!"); } while (ReceivalBuffer.Remaining >= 4) { int length = AesCryptograph.RetrieveLength(ReceivalBuffer.ReadBytes(4)); if (ReceivalBuffer.Remaining < length) { ReceivalBuffer.Position -= 4; try { Buffer.BlockCopy(ReceivalBuffer.Array, ReceivalBuffer.Position, ReceivalBuffer.Array, 0, ReceivalBuffer.Remaining); } catch (Exception e) { Log.SkipLine(); Tracer.TraceErrorMessage(e, "Failed to BlockCopy!"); Log.SkipLine(); throw; } reset = true; break; } ReceivalBuffer.Position -= 4; HandleIncomingPacket(ReceivalBuffer.ReadBytes(length + 4)); processed += (length + 4); } ReceivalBuffer.Limit -= processed; ReceivalBuffer.Position = reset ? 0 : ReceivalBuffer.Limit; } else { HandleIncomingPacket(ReceivalBuffer.GetContent()); ReceivalBuffer.Limit = 0; ReceivalBuffer.Position = 0; } } else { Stop(); } ReceiveDone.Set(); } catch (ArgumentNullException ex) { Log.SkipLine(); Tracer.TraceErrorMessage(ex, "Argument is null!"); Log.SkipLine(); throw; } catch (ArgumentOutOfRangeException ex) { Log.SkipLine(); Tracer.TraceErrorMessage(ex, "Argument is out of range!"); Log.SkipLine(); throw; } catch (SocketException ex) { Log.SkipLine(); Tracer.TraceErrorMessage(ex, "Socket exception!"); Log.SkipLine(); throw; } catch (Exception ex) { Log.SkipLine(); Log.Error("Uncatched fatal error on {0}: ", ex, Title.ToLower(), Thread.CurrentThread.ManagedThreadId); Stop(); } }