예제 #1
0
        private void AcceptCallback(IAsyncResult ar)
        {
            mainDone.Set();

            Socket listener = (Socket)ar.AsyncState;
            Socket handler;

            try
            {
                handler = listener.EndAccept(ar);
            }
            catch { return; }

            if (OnConnect != null)
            {
                OnConnect(this, handler);
            }

            ServerState state = new ServerState();

            state.workSocket = handler;
            state.endPoint   = (IPEndPoint)handler.RemoteEndPoint;
            stateObjectDictionary.Add(state, state.workSocket);
            try
            {
                state.dynamicBuffer = new byte[state.bufferSize];
                handler.BeginReceive(state.dynamicBuffer, 0, state.bufferSize, 0,
                                     new AsyncCallback(ReadCallback), state);
            }
            catch (SocketException)
            {
            }
            catch (Exception ex)
            {
                if (OnError != null)
                {
                    OnError(this, ex.ToString());
                }
            }
        }
예제 #2
0
        private void AcceptCallback(IAsyncResult ar)
        {
            mainDone.Set();

            Socket listener = (Socket) ar.AsyncState;
            Socket handler;
            try
            {
                handler = listener.EndAccept(ar);
            }
            catch { return; }

            if (OnConnect != null)
                OnConnect(this, handler);

            ServerState state = new ServerState();

            state.workSocket = handler;
            state.endPoint = (IPEndPoint) handler.RemoteEndPoint;
            stateObjectDictionary.Add(state, state.workSocket);
            try
            {
                state.dynamicBuffer = new byte[state.bufferSize];
                handler.BeginReceive(state.dynamicBuffer, 0, state.bufferSize, 0,
                    new AsyncCallback(ReadCallback), state);
            }
            catch (SocketException)
            {
            }
            catch (Exception ex)
            {
                if (OnError != null)
                    OnError(this, ex.ToString());
            }
        }
예제 #3
0
        public void ReadCallback(IAsyncResult ar)
        {
            String      content = String.Empty;
            ServerState state   = (ServerState)ar.AsyncState;
            Socket      handler = state.workSocket;

            int bytesRead = 0;

            try
            {
                bytesRead = handler.EndReceive(ar);
            }
            catch (SocketException)
            {
                // Connection closed by peer
                if (OnDisconnect != null)
                {
                    OnDisconnect(this, state.endPoint);
                }
                return;
            }
            catch (ObjectDisposedException)
            {
                // Connection closed by client
                if (OnDisconnect != null)
                {
                    OnDisconnect(this, state.endPoint);
                }
                return;
            }
            catch (Exception e)
            {
                if (OnError != null)
                {
                    OnError(this, e.ToString());
                }
                return;
            }

            if (bytesRead > 0)
            {
                byte[]      data      = state.dynamicBuffer;
                PacketBytes firstByte = (PacketBytes)data[0];
                PacketSizes size      = PacketSizes.Null;

                if (state.result == null)
                {
                    switch (firstByte)
                    {
                    case PacketBytes.KeepAlive_0x00:
                        size = PacketSizes.KeepAlive_0x00;
                        break;

                    case PacketBytes.Login_0x01:
                        size = PacketSizes.Login_0x01;
                        break;

                    case PacketBytes.Handshake_0x02:
                        size = PacketSizes.Handshake_0x02;
                        break;

                    case PacketBytes.Chat_0x03:
                        size = PacketSizes.Chat_0x03;
                        break;

                    case PacketBytes.UpdateTime_0x04:
                        size = PacketSizes.UpdateTime_0x04;
                        break;

                    case PacketBytes.Unkown01_0x0A:
                        size = PacketSizes.Unkown01_0x0A;
                        break;

                    case PacketBytes.PlayerPosition_0x0B:
                        size = PacketSizes.PlayerPosition_0x0B;
                        break;

                    case PacketBytes.PlayerLook_0x0C:
                        size = PacketSizes.PlayerLook_0x0C;
                        break;

                    case PacketBytes.PlayerMoveAndLook_0x0D:
                        size = PacketSizes.PlayerMoveAndLook_0x0D;
                        break;

                    case PacketBytes.PlaceBlock_0x0F:
                        size = PacketSizes.PlaceBlock_0x0F;
                        break;

                    case PacketBytes.BlockItemSwitch_0x10:
                        size = PacketSizes.BlockItemSwitch_0x10;
                        break;

                    case PacketBytes.AddToInventory_0x11:
                        size = PacketSizes.AddToInventory_0x11;
                        break;

                    case PacketBytes.ArmAnimation_0x12:
                        size = PacketSizes.ArmAnimation_0x12;
                        break;

                    case PacketBytes.EntitySpawnName_0x14:
                        size = PacketSizes.EntitySpawnName_0x14;
                        break;

                    case PacketBytes.EntitySpawn_0x15:
                        size = PacketSizes.EntitySpawn_0x15;
                        break;

                    case PacketBytes.CollectItem_0x16:
                        size = PacketSizes.CollectItem_0x16;
                        break;

                    case PacketBytes.Unkown02_0x17:
                        size = PacketSizes.Unkown02_0x17;
                        break;

                    case PacketBytes.MobSpawn_0x18:
                        size = PacketSizes.MobSpawn_0x18;
                        break;

                    case PacketBytes.DestroyEntity_0x1D:
                        size = PacketSizes.DestroyEntity_0x1D;
                        break;

                    case PacketBytes.Entity_0x1E:
                        size = PacketSizes.Entity_0x1E;
                        break;

                    case PacketBytes.RelativeEntityMove_0x1F:
                        size = PacketSizes.RelativeEntityMove_0x1F;
                        break;

                    case PacketBytes.EntityLook_0x20:
                        size = PacketSizes.EntityLook_0x20;
                        break;

                    case PacketBytes.RelativeEntityLookAndMove_0x21:
                        size = PacketSizes.RelativeEntityLookAndMove_0x21;
                        break;

                    case PacketBytes.EntityTeleport_0x22:
                        size = PacketSizes.EntityTeleport_0x22;
                        break;

                    case PacketBytes.ChunkPre_0x32:
                        size = PacketSizes.ChunkPre_0x32;
                        break;

                    case PacketBytes.ChunkMap_0x33:
                        size = PacketSizes.ChunkMap_0x33;
                        break;

                    case PacketBytes.MultiBlockChange_0x34:
                        size = PacketSizes.MultiBlockChange_0x34;
                        break;

                    case PacketBytes.BlockChange_0x35:
                        size = PacketSizes.BlockChange_0x35;
                        break;

                    case PacketBytes.Disconnect_0xFF:
                        size = PacketSizes.Disconnect_0xFF;
                        break;
                    }

                    if (size == PacketSizes.String)
                    {
                        if (state.result == null)
                        {
                            state.result = new StringResult(this, handler, state, firstByte, data, 2);
                            state.result.Receive(); // erster einsprung
                        }
                        else
                        {
                            if (state.result.HasResult)
                            {
                                String result = (state.result as StringResult).ResultString;
                                state.result = null;
                            }
                            else
                            {
                                state.result.Receive(); // nächster einsprung
                            }
                        }
                    }

                    if (size == PacketSizes.String)
                    {
                        if (state.result == null)
                        {
                            state.result = new StringResult(this, handler, state, firstByte, data, bytesRead);
                            state.result.Receive(); // erster einsprung
                        }
                        else
                        {
                            if (state.result.HasResult)
                            {
                                String result = (state.result as StringResult).ResultString;
                                state.result = null;
                            }
                            else
                            {
                                state.result.Receive(); // nächster einsprung
                            }
                        }
                    }
                }

                if (OnByteDataAvailable != null)
                {
                    OnByteDataAvailable(this, handler, firstByte, data, bytesRead);
                }
            }
            else
            {
                // Connection closed by peer
                if (OnDisconnect != null)
                {
                    OnDisconnect(this, state.endPoint);
                }
            }
        }