示例#1
0
        public channel connect(String ip, short port)
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                s.Connect(new IPEndPoint(IPAddress.Parse(ip), port));

                channel ch = new channel(s);
                ch.onDisconnect += this.onChannelDisconn;

                process_.reg_channel(ch);

                return(ch);
            }
            catch (System.Net.Sockets.SocketException e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Net.Sockets.SocketException:{0}", e);

                return(null);
            }
            catch (System.Exception e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Exceptio:{0}", e);

                return(null);
            }
        }
示例#2
0
 public void onChannelDisconn(channel ch)
 {
     if (onChannelDisconnect != null)
     {
         onChannelDisconnect(ch);
     }
 }
示例#3
0
        public channel connect(String ip, short port)
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            s.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0));
            try
            {
                s.Connect(new IPEndPoint(IPAddress.Parse(ip), port));

                channel ch = new channel(s);
                ch.onDisconnect += this.onChannelDisconn;

                process_.reg_channel(ch);

                return ch;
            }
            catch (System.Net.Sockets.SocketException e)
            {
                System.Console.WriteLine("System.Net.Sockets.SocketException{0}", e);

                return null;
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine("System.Exceptio{0}", e);

                return null;
            }
        }
示例#4
0
 public void onChannelDisconn(channel ch)
 {
     if (onChannelDisconnect != null)
     {
         onChannelDisconnect(ch);
     }
 }
示例#5
0
        public channel connect(String ip, short port)
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                s.Connect(new IPEndPoint(IPAddress.Parse(ip), port));

                channel ch = new channel(s);
                ch.onDisconnect += this.onChannelDisconn;

                process_.reg_channel(ch);

                return(ch);
            }
            catch (System.Net.Sockets.SocketException e)
            {
                System.Console.WriteLine("System.Net.Sockets.SocketException{0}", e);

                return(null);
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine("System.Exceptio{0}", e);

                return(null);
            }
        }
示例#6
0
        private void send_callback(IAsyncResult ar)
        {
            channel ch = ar.AsyncState as channel;

            try
            {
                int send = ch.s.EndSend(ar);

                lock (send_buff)
                {
                    if (send_buff.Count <= 0)
                    {
                        _send_state = send_state.idel;
                    }
                    else
                    {
                        var data = (byte[])send_buff.Dequeue();
                        s.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(this.send_callback), this);
                    }
                }
            }
            catch (System.Net.Sockets.SocketException e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Net.Sockets.SocketException:{0}", e);

                onDisconnect(this);
            }
            catch (System.Exception e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Exception:{0}", e);

                s.Close();
                onDisconnect(this);
            }
        }
示例#7
0
        public void onChannelConn(channel ch)
        {
            process_.reg_channel(ch);

            if (onChannelConnect != null)
            {
                onChannelConnect(ch);
            }
        }
示例#8
0
        public void onChannelDisconn(channel ch)
        {
            if (onChannelDisconnect != null)
            {
                onChannelDisconnect(ch);
            }

            process_.unreg_channel(ch);
        }
        public void onChannelConn(channel ch)
        {
            process_.reg_channel(ch);

            if (onChannelConnect != null)
            {
                onChannelConnect(ch);
            }
        }
示例#10
0
        public void onChannelDisconn(channel ch)
        {
            if (onChannelDisconnect != null)
            {
                onChannelDisconnect(ch);
            }

            process_.unreg_channel(ch);
        }
        public void onChannelDisconn(channel ch)
        {
            ch.s.Dispose();

            if (onChannelDisconnect != null)
            {
                onChannelDisconnect(ch);
            }

            process_.unreg_channel(ch);
        }
示例#12
0
        private void onAccept(IAsyncResult ar)
        {
            TcpListener listener = ar.AsyncState as TcpListener;

            Socket clientSocket = listener.EndAcceptSocket(ar);

            {
                channel ch = new channel(clientSocket);
                ch.onDisconnect += this.onChannelDisconn;

                onChannelConn(ch);
            }

            listener.BeginAcceptSocket(new AsyncCallback(this.onAccept), listener);
        }
        private void onAccept(IAsyncResult ar)
        {
            TcpListener listener = ar.AsyncState as TcpListener;

            Socket clientSocket = listener.EndAcceptSocket(ar);

            {
                channel ch = new channel(clientSocket);
                ch.onDisconnect += this.onChannelDisconn;

                onChannelConn(ch);
            }

            listener.BeginAcceptSocket(new AsyncCallback(this.onAccept), listener);
        }
示例#14
0
        private void send_callback(IAsyncResult ar)
        {
            channel ch = ar.AsyncState as channel;

            try
            {
                int send = ch.s.EndSend(ar);

                lock (send_buff)
                {
                    send_len += send;
                    if (send_len < tmp_send_buff.Length)
                    {
                        s.BeginSend(tmp_send_buff, send_len, tmp_send_buff.Length - send_len, SocketFlags.None, new AsyncCallback(this.send_callback), this);
                    }
                    else if (send_len == tmp_send_buff.Length)
                    {
                        if (send_buff.Count <= 0)
                        {
                            _send_state = send_state.idel;
                        }
                        else
                        {
                            var data = (byte[])send_buff.Dequeue();
                            tmp_send_buff = data;
                            send_len      = 0;
                            s.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(this.send_callback), this);
                        }
                    }
                }
            }
            catch (System.ObjectDisposedException)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "socket is release");
            }
            catch (System.Net.Sockets.SocketException)
            {
            }
            catch (System.Exception e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Exception:{0}", e);
            }
        }
示例#15
0
        private void onAccept(IAsyncResult ar)
        {
            TcpListener listener = ar.AsyncState as TcpListener;

            try
            {
                Socket  clientSocket = listener.EndAcceptSocket(ar);
                channel ch           = new channel(clientSocket);
                ch.onDisconnect += this.onChannelDisconn;
                ch.Disconnect   += this.ChannelDisconn;

                onChannelConn(ch);
            }
            catch (System.Exception e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Exception:{0}", e);
            }

            listener.BeginAcceptSocket(new AsyncCallback(this.onAccept), listener);
        }
示例#16
0
        private void onRead(IAsyncResult ar)
        {
            channel ch = ar.AsyncState as channel;

            try
            {
                int read = ch.s.EndReceive(ar);

                if (read > 0)
                {
                    log.log.trace(new System.Diagnostics.StackFrame(), timerservice.Tick, "recv data len:{0}", read);

                    MemoryStream st = new MemoryStream();
                    if (tmpbufoffset > 0)
                    {
                        st.Write(tmpbuf, 0, tmpbufoffset);
                    }
                    st.Write(recvbuf, 0, read);
                    st.Position = 0;
                    byte[] data     = st.ToArray();
                    int    data_len = tmpbufoffset + read;

                    tmpbuf       = null;
                    tmpbufoffset = 0;

                    int offset = 0;
                    while (true)
                    {
                        int over_len = data_len - offset;
                        if (over_len < 4)
                        {
                            break;
                        }

                        Int32 len = ((Int32)data[offset + 0]) | ((Int32)data[offset + 1]) << 8 | ((Int32)data[offset + 2]) << 16 | ((Int32)data[offset + 3]) << 24;
                        if (over_len < len + 4)
                        {
                            break;
                        }
                        offset += 4;

                        MemoryStream _tmp = new MemoryStream();
                        _tmp.Write(data, offset, len);
                        _tmp.Position = 0;
                        var json = System.Text.Encoding.UTF8.GetString(_tmp.ToArray());
                        log.log.trace(new System.Diagnostics.StackFrame(true), timerservice.Tick, "len:{0} msg:{1}", len, json);
                        try
                        {
                            ArrayList unpackedObject = (ArrayList)Json.Jsonparser.unpack(json);

                            lock (que)
                            {
                                que.Enqueue(unpackedObject);
                            }
                        }
                        catch (Exception e)
                        {
                            log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "msg:{0}, System.Exception:{1}", json, e);
                        }
                        offset += len;
                    }

                    int overplus_len = data_len - offset;
                    st = new MemoryStream();
                    st.Write(data, offset, overplus_len);
                    st.Position  = 0;
                    tmpbuf       = st.ToArray();
                    tmpbufoffset = overplus_len;
                }

                ch.s.BeginReceive(recvbuf, 0, recvbuflenght, 0, new AsyncCallback(this.onRead), this);
            }
            catch (System.ObjectDisposedException)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "socket is release");
            }
            catch (System.Net.Sockets.SocketException)
            {
                if (onDisconnect != null)
                {
                    onDisconnect(this);
                }
            }
            catch (System.Exception e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Exception:{0}", e);

                ch.s.Close();
                if (onDisconnect != null)
                {
                    onDisconnect(this);
                }
            }
        }
示例#17
0
 public void ChannelDisconn(channel ch)
 {
     process_.unreg_channel(ch);
 }
示例#18
0
        private void onRead(IAsyncResult ar)
        {
            channel ch = ar.AsyncState as channel;

            try
            {
                int read = ch.s.EndReceive(ar);

                if (read > 0)
                {
                    if (tmpbufoffset == 0)
                    {
                        int offset = 0;
                        do
                        {
                            Int32 len = ((Int32)recvbuf[offset + 0]) | ((Int32)recvbuf[offset + 1]) << 8 | ((Int32)recvbuf[offset + 2]) << 16 | ((Int32)recvbuf[offset + 3]) << 24;

                            if (len <= (read - 4))
                            {
                                read   -= len + 4;
                                offset += 4;

                                MemoryStream _tmp = new MemoryStream();

                                _tmp.Write(recvbuf, offset, len);
                                offset += len;

                                _tmp.Position = 0;

                                ArrayList unpackedObject = (ArrayList)System.Text.Json.Jsonparser.unpack(System.Text.Encoding.Default.GetString(_tmp.ToArray()));

                                lock (que)
                                {
                                    que.Enqueue(unpackedObject);
                                }
                            }
                            else
                            {
                                if (tmpbuflenght == 0)
                                {
                                    tmpbuflenght = recvbuflenght * 2;
                                    tmpbuf       = new byte[tmpbuflenght];
                                }

                                while ((tmpbuflenght - tmpbufoffset) < read)
                                {
                                    byte[] newtmpbuf = new byte[2 * tmpbuflenght];
                                    tmpbuf.CopyTo(newtmpbuf, 0);
                                    tmpbuf = newtmpbuf;
                                }

                                MemoryStream _tmp = new MemoryStream();
                                _tmp.Write(recvbuf, offset, read);

                                _tmp.ToArray().CopyTo(tmpbuf, tmpbufoffset);
                                tmpbufoffset = read;

                                break;
                            }
                        } while (true);
                    }
                    else if (tmpbufoffset > 0)
                    {
                        while ((tmpbuflenght - tmpbufoffset) < read)
                        {
                            byte[] newtmpbuf = new byte[2 * tmpbuflenght];
                            tmpbuf.CopyTo(newtmpbuf, 0);
                            tmpbuf = newtmpbuf;
                        }

                        MemoryStream _tmp_ = new MemoryStream();
                        _tmp_.Write(recvbuf, 0, read);

                        _tmp_.ToArray().CopyTo(tmpbuf, tmpbufoffset);
                        tmpbufoffset += (Int32)_tmp_.Length;

                        int offset = 0;
                        do
                        {
                            Int32 len = ((Int32)tmpbuf[offset + 0]) | ((Int32)tmpbuf[offset + 1]) << 8 | ((Int32)tmpbuf[offset + 2]) << 16 | ((Int32)tmpbuf[offset + 3]) << 24;

                            if (len <= (tmpbufoffset - 4))
                            {
                                tmpbufoffset -= len + 4;
                                offset       += 4;

                                MemoryStream _tmp = new MemoryStream();

                                _tmp.Write(tmpbuf, offset, len);
                                offset += len;

                                _tmp.Position = 0;

                                ArrayList unpackedObject = (ArrayList)System.Text.Json.Jsonparser.unpack(_tmp.ToString());

                                lock (que)
                                {
                                    que.Enqueue(unpackedObject);
                                }
                            }
                            else
                            {
                                MemoryStream _tmp = new MemoryStream();
                                _tmp.Write(tmpbuf, offset, tmpbufoffset);

                                _tmp.ToArray().CopyTo(tmpbuf, 0);

                                break;
                            }
                        } while (true);
                    }

                    ch.s.BeginReceive(recvbuf, 0, recvbuflenght, 0, new AsyncCallback(this.onRead), this);
                }
                else
                {
                    ch.s.Close();
                    onDisconnect(ch);
                }
            }
            catch (System.Net.Sockets.SocketException)
            {
                ch.s.Close();
                onDisconnect(ch);
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine("System.Exceptio{0}", e);

                ch.s.Close();
                onDisconnect(ch);
            }
        }
示例#19
0
        private void onRead(IAsyncResult ar)
        {
            channel ch = ar.AsyncState as channel;

            try
            {
                int read = ch.s.EndReceive(ar);
                if (read > 0)
                {
                    if (tmpbufoffset == 0)
                    {
                        int offset = 0;
                        do
                        {
                            if (read > 4)
                            {
                                Int32 len = ((Int32)recvbuf[offset + 0]) | ((Int32)recvbuf[offset + 1]) << 8 | ((Int32)recvbuf[offset + 2]) << 16 | ((Int32)recvbuf[offset + 3]) << 24;

                                if (len <= (read - 4))
                                {
                                    read   -= len + 4;
                                    offset += 4;

                                    MemoryStream _tmp = new MemoryStream();

                                    _tmp.Write(recvbuf, offset, len);
                                    offset += len;

                                    _tmp.Position = 0;

                                    var json = System.Text.Encoding.UTF8.GetString(_tmp.ToArray());
                                    try
                                    {
                                        ArrayList unpackedObject = (ArrayList)Json.Jsonparser.unpack(json);

                                        Monitor.Enter(que);
                                        que.Enqueue(unpackedObject);
                                        Monitor.Exit(que);
                                    }
                                    catch (Exception e)
                                    {
                                        log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "msg:{0}, System.Exception:{1}", json, e);
                                    }
                                }
                                else
                                {
                                    if (tmpbuflenght == 0)
                                    {
                                        tmpbuflenght = recvbuflenght * 2;
                                        tmpbuf       = new byte[tmpbuflenght];
                                    }

                                    while ((tmpbuflenght - tmpbufoffset) < read)
                                    {
                                        byte[] newtmpbuf = new byte[2 * tmpbuflenght];
                                        tmpbuf.CopyTo(newtmpbuf, 0);
                                        tmpbuf = newtmpbuf;
                                    }

                                    MemoryStream _tmp = new MemoryStream();
                                    _tmp.Write(recvbuf, offset, read);

                                    _tmp.ToArray().CopyTo(tmpbuf, tmpbufoffset);
                                    tmpbufoffset = read;

                                    break;
                                }
                            }
                            else
                            {
                                if (read > 0)
                                {
                                    if (tmpbuflenght == 0)
                                    {
                                        tmpbuflenght = recvbuflenght * 2;
                                        tmpbuf       = new byte[tmpbuflenght];
                                    }

                                    while ((tmpbuflenght - tmpbufoffset) < read)
                                    {
                                        byte[] newtmpbuf = new byte[2 * tmpbuflenght];
                                        tmpbuf.CopyTo(newtmpbuf, 0);
                                        tmpbuf = newtmpbuf;
                                    }

                                    MemoryStream _tmp = new MemoryStream();
                                    _tmp.Write(recvbuf, offset, read);

                                    _tmp.ToArray().CopyTo(tmpbuf, tmpbufoffset);
                                    tmpbufoffset = read;
                                }
                                break;
                            }
                        } while (true);
                    }
                    else if (tmpbufoffset > 0)
                    {
                        while ((tmpbuflenght - tmpbufoffset) < read)
                        {
                            byte[] newtmpbuf = new byte[2 * tmpbuflenght];
                            tmpbuf.CopyTo(newtmpbuf, 0);
                            tmpbuf = newtmpbuf;
                        }

                        MemoryStream _tmp_ = new MemoryStream();
                        _tmp_.Write(recvbuf, 0, read);

                        _tmp_.ToArray().CopyTo(tmpbuf, tmpbufoffset);
                        tmpbufoffset += (Int32)_tmp_.Length;

                        int offset = 0;
                        do
                        {
                            if (tmpbufoffset > 4)
                            {
                                Int32 len = ((Int32)tmpbuf[offset + 0]) | ((Int32)tmpbuf[offset + 1]) << 8 | ((Int32)tmpbuf[offset + 2]) << 16 | ((Int32)tmpbuf[offset + 3]) << 24;

                                if (len <= (tmpbufoffset - 4))
                                {
                                    tmpbufoffset -= len + 4;
                                    offset       += 4;

                                    MemoryStream _tmp = new MemoryStream();

                                    _tmp.Write(tmpbuf, offset, len);
                                    offset += len;

                                    _tmp.Position = 0;

                                    var json = System.Text.Encoding.UTF8.GetString(_tmp.ToArray());
                                    try
                                    {
                                        ArrayList unpackedObject = (ArrayList)Json.Jsonparser.unpack(json);

                                        Monitor.Enter(que);
                                        que.Enqueue(unpackedObject);
                                        Monitor.Exit(que);
                                    }
                                    catch (Exception e)
                                    {
                                        log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "msg:{0}, System.Exception:{1}", json, e);
                                    }
                                }
                                else
                                {
                                    MemoryStream _tmp = new MemoryStream();
                                    _tmp.Write(tmpbuf, offset, tmpbufoffset);

                                    _tmp.ToArray().CopyTo(tmpbuf, 0);

                                    break;
                                }
                            }
                            else
                            {
                                if (tmpbufoffset > 0)
                                {
                                    MemoryStream _tmp = new MemoryStream();
                                    _tmp.Write(tmpbuf, offset, tmpbufoffset);

                                    _tmp.ToArray().CopyTo(tmpbuf, 0);
                                }
                                break;
                            }
                        } while (true);
                    }

                    ch.s.BeginReceive(recvbuf, 0, recvbuflenght, 0, new AsyncCallback(this.onRead), this);
                }
                else
                {
                    ch.s.Close();
                    onDisconnect(ch);
                }
            }
            catch (System.Exception e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Exception:{0}", e);

                onDisconnect(ch);
            }
        }