public Package ReadPackage(ref int srcOffset, ref int pkgLength, int curBufferSize, byte[] srcBuffer, ByteSocket sock) { short header = 0; while (srcOffset + 4 < curBufferSize) { header = (short)((srcBuffer[srcOffset] << 8) + srcBuffer[srcOffset + 1]); if (header == Package.HEADER) { pkgLength = (srcBuffer[srcOffset + 2] << 8) + srcBuffer[srcOffset + 3]; break; } else { srcOffset++; } } int dataLeft = curBufferSize - srcOffset; if ((pkgLength != 0 && pkgLength < Package.HDR_SIZE) || pkgLength > sock.ReceiveBufferSize) { return null; } if (dataLeft >= pkgLength && pkgLength != 0) { Package pkg = sock.ReceiveUsedPacket; pkg.CopyFrom(srcBuffer, srcOffset, 0, pkgLength); return pkg; } return null; }
public PackageProcessor(ByteSocket socket, byte[] sBuffer, byte[] rBuffer) { // TODO: Complete member initialization m_sendBuffer = sBuffer; m_receiveBuffer = rBuffer; Init(socket); AsyncReceiveImp(); }
//protected IPackageReader pkgReader; public PackageProcessor(ByteSocket socket) { // TODO: Complete member initialization m_sendBuffer = new byte[m_socket.SendBufferSize]; m_receiveBuffer = new byte[m_socket.ReceiveBufferSize]; Init(socket); AsyncReceiveImp(); }
static void Main(string[] args) { ResourceUtil.ExtractResource("LogConfig.xml", "LogConfig.xml", Assembly.GetAssembly(typeof(Program))); ByteSocket bs = new ByteSocket(); bs.ConnectSuccessHandler += new ByteSocket.ConnectSocketDelegate(bs_ConnectSuccessHandler); bs.Connect("192.168.16.119", 20000); Console.ReadKey(); }
private void m_socket_ConnectSuccessHandler(ByteSocket bSock) { OnConnected(true); }
private void m_socket_ConnectFailedHandler(ByteSocket bSock) { OnConnected(false); }
public JSNPkgProcessor(ByteSocket socket, byte[] sBuffer, byte[] rBuffer) : base(socket, sBuffer, rBuffer) { }
protected virtual void Init(ByteSocket socket) { m_sendingTcp = false; m_pkgQueue = new Queue(256); //pkgReader = new PackageReader(); m_socket = socket; m_sendAsyncEvent = new SocketAsyncEventArgs(); m_sendAsyncEvent.UserToken = this; m_sendAsyncEvent.SetBuffer(m_sendBuffer, 0, 0); m_sendAsyncEvent.Completed += new EventHandler<SocketAsyncEventArgs>(AsyncSendPkgComplete); m_receiveAsyncEvent = new SocketAsyncEventArgs(); m_receiveAsyncEvent.Completed += new EventHandler<SocketAsyncEventArgs>(AsyncReceiveComplete); }
static void bs_ConnectSuccessHandler(ByteSocket bSock) { Console.WriteLine("客户端启动成功..."); bSock.Sockets.Send(new byte[0]); }