示例#1
0
    public void Connect()
    {
        serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
        endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), 2500);

        resolver = new PacketResolver();

        connectEventArgs = new SocketAsyncEventArgs();

        sendEventArgs = new SocketAsyncEventArgs();
        recvEventArgs = new SocketAsyncEventArgs();

        connectEventArgs.RemoteEndPoint = endPoint;
        connectEventArgs.Completed     += new System.EventHandler <SocketAsyncEventArgs>(OnConnect);

        sendEventArgs.SetBuffer(new byte[Config.MAX_SESSION_BUFFER_SIZE], 0, Config.MAX_SESSION_BUFFER_SIZE);
        recvEventArgs.SetBuffer(new byte[Config.MAX_SESSION_BUFFER_SIZE], 0, Config.MAX_SESSION_BUFFER_SIZE);

        sendEventArgs.Completed += OnSend;
        recvEventArgs.Completed += OnReceive;

        serverSocket.ConnectAsync(connectEventArgs);

        GameObject.DontDestroyOnLoad(this.gameObject);
    }
示例#2
0
 public CustomUserToken()
 {
     m_lockQueue      = new object();
     m_packetResolver = new PacketResolver();
     m_peer           = null;
     m_sendQueue      = new Queue <CustomPacket>();
 }
    // Called when data has been read from the stream.
    private void OnRead(IAsyncResult result)
    {
        stream.EndRead(result);

        // If the length of the buffer is 5, we should process the packet header.
        if (inBuffer.Length == 5)
        {
            using (PacketReader reader = new PacketReader(new MemoryStream(inBuffer)))
            {
                // Skip the packet ID since we don't need it now.
                reader.ReadByte();

                // Read the length from the packet header.
                int newLength = reader.ReadInt32();

                // Resize the packet buffer to the new length.
                var tmp = new byte[newLength];
                if (newLength < 5)
                {
                    Disconnect();
                    return;
                }
                inBuffer.CopyTo(tmp, 0);
                inBuffer = tmp;
            }

            // Try to read the enough bytes to fill the packet buffer.
            // We already have 5 bytes in the buffer, so we read inBuffer.Length - 5.
            stream.BeginRead(inBuffer, 5, inBuffer.Length - 5, OnRead, null);
        }
        // If the length is not 5, we have a whole packet that we should process.
        else
        {
            using (PacketReader reader = new PacketReader(new MemoryStream(inBuffer)))
            {
                // Read the packet header.
                byte id     = reader.ReadByte();
                int  length = reader.ReadInt32();

                // Create a new instance of the packet we received.
                IServerPacket packet = PacketResolver.Create((PacketType)id);
                if (packet != null)
                {
                    // Read the packet and print its info.
                    packet.Read(reader);
                    // Debug.Log(string.Format("{0}, {1}", id, length));
                    // Debug.Log(packet.ToString());
                    if (OnPacket != null)
                    {
                        OnPacket.Invoke(packet);
                    }
                }
                else
                {
                    // We don't have a definition for the packet type we
                    // received, so we cannot instantiate an instance of it.
                    Debug.Log(string.Format("Unable to create packet {0}", id));
                }
            }

            // Reset the buffer so we are ready to receive the next packet header.
            inBuffer = new byte[5];
            stream.BeginRead(inBuffer, 0, 5, OnRead, null);
        }
    }