当前位置: 首页>>代码示例>>C#>>正文


C# NetworkStream.DataReceive方法代码示例

本文整理汇总了C#中NetworkStream.DataReceive方法的典型用法代码示例。如果您正苦于以下问题:C# NetworkStream.DataReceive方法的具体用法?C# NetworkStream.DataReceive怎么用?C# NetworkStream.DataReceive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NetworkStream的用法示例。


在下文中一共展示了NetworkStream.DataReceive方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Handshaker

        private static void Handshaker(object param)
        {
            int id = (int)((object[])param) [0];
            Socket socket = (Socket)((object[])param) [1];

            using (socket) {
                using (NetworkStream stream = new NetworkStream(socket)) {
                    InitMessage initMsg = stream.DataReceive<InitMessage> ();
                    switch (initMsg.Action) {
                    case InitAction.LoadMono:
                        if (initMsg.Parameter != null) {
                            LoadMono (stream, initMsg.Parameter as string);
                        } else {
                            LoadMono (stream);
                        }

                        break;
                    case InitAction.Start:
                        TypeInfo msg = stream.DataReceive<TypeInfo> ();
                        if (msg.Type != null)
                            ServiceStarter (id, stream, msg.Type);
                        else
                            stream.DataSend (new OperationResultMessage (
                                new ArgumentException (string.Concat ("could not find requested service <", msg.TypeName, "> in assembly <", msg.AssemblyName, ">"))));
                        break;
                    case InitAction.Stream:
                        Stream (id, stream, stream.DataReceive<StreamAvaliableMessage> ().Id);
                        break;
                    case InitAction.Close:
                        run = false;
                        break;
                    }
                }
            }
        }
开发者ID:xxtbg,项目名称:coredroidservice,代码行数:35,代码来源:SocketService.cs

示例2: Stream

        private static void Stream(int id, NetworkStream stream, int streamId)
        {
            Stream streamToSend = waitingStreams [streamId];

            bool closed = false;
            while (!closed) {
                StreamActionMessage message = stream.DataReceive<StreamActionMessage> ();

                try {
                    switch (message.Action) {
                    case StreamAction.CanRead:
                        bool canRead = streamToSend.CanRead;
                        stream.ActionFinishedSuccess ();
                        stream.DataSend (canRead);
                        break;
                    case StreamAction.CanSeek:
                        bool canSeek = streamToSend.CanSeek;
                        stream.ActionFinishedSuccess ();
                        stream.DataSend (canSeek);
                        break;
                    case StreamAction.CanWrite:
                        bool canWrite = streamToSend.CanWrite;
                        stream.ActionFinishedSuccess ();
                        stream.DataSend (canWrite);
                        break;
                    case StreamAction.Length:
                        long length = streamToSend.Length;
                        stream.ActionFinishedSuccess ();
                        stream.DataSend (length);
                        break;
                    case StreamAction.GetPosition:
                        long position = streamToSend.Position;
                        stream.ActionFinishedSuccess ();
                        stream.DataSend (position);
                        break;
                    case StreamAction.SetPosition:
                        streamToSend.Position = message.Position;
                        stream.ActionFinishedSuccess ();
                        break;
                    case StreamAction.Close:
                        closed = true;
                        streamToSend.Close ();
                        stream.DataSend (new OperationResultMessage (true));
                        break;
                    case StreamAction.Flush:
                        streamToSend.Flush ();
                        stream.DataSend (new OperationResultMessage (true));
                        break;
                    case StreamAction.Read:
                        byte[] buffer = new byte[Convert.ToInt32 (message.Size)];
                        streamToSend.Read (buffer, 0, Convert.ToInt32 (message.Size));
                        stream.DataSend (buffer.Take (Convert.ToInt32 (message.Size)).ToArray ());
                        stream.DataSend (new OperationResultMessage (true));
                        break;
                    case StreamAction.Seek:
                        long seeked = streamToSend.Seek (message.Offset, message.Position == 0 ? SeekOrigin.Begin : (message.Position == 1 ? SeekOrigin.Current : SeekOrigin.End));
                        stream.DataSend (new OperationResultMessage (true));
                        stream.DataSend (seeked);
                        break;
                    case StreamAction.SetLength:
                        streamToSend.SetLength (message.Size);
                        stream.DataSend (new OperationResultMessage (true));
                        break;
                    case StreamAction.Write:
                        byte[] data = stream.DataReceive<byte[]> ();
                        streamToSend.Write (data, 0, data.Length);
                        stream.DataSend (new OperationResultMessage (true));
                        break;
                    default:
                        throw (new NotImplementedException ());
                    }
                } catch (Exception ex) {
                    stream.DataSend (new OperationResultMessage (ex));
                }
            }

            lock (waitingStreams) {
                waitingStreams.Remove (id);
            }

            ServiceStop (id, null);
        }
开发者ID:xxtbg,项目名称:coredroidservice,代码行数:82,代码来源:SocketService.cs

示例3: ServiceHandleCall

        private static void ServiceHandleCall(NetworkStream stream, object service)
        {
            ServiceCallMessage msg = stream.DataReceive<ServiceCallMessage> ();
            string methodName = msg.ChildName;
            TypeInfo[] parameterInfos = msg.Parameter;

            List<object > parameters = new List<object> ();

            foreach (TypeInfo parameterInfo in parameterInfos) {
                if (!parameterInfo.IsNull)
                    parameters.Add (stream.DataReceive (parameterInfo.Type));
                else
                    parameters.Add (null);
            }

            System.Reflection.MethodInfo methodInfo = service.GetType ().GetMethod (methodName);
            if (methodInfo.GetCustomAttributes (true).Where (a => a is ServiceMemberAttribute).Any ()) {
                try {
                    object returnValue = methodInfo.Invoke (service, parameters.ToArray ());

                    if (methodInfo.ReturnType != null) {
                        stream.DataSend (new OperationResultMessage (true));
                        if (returnValue != null) {
                            stream.DataSend (new TypeInfo (returnValue.GetType ()));
                        } else {
                            stream.DataSend (new TypeInfo (null));
                        }

                        if (returnValue != null) {
                            if (returnValue is Stream) {
                                SendStream (stream, (Stream)returnValue);
                            } else {
                                stream.DataSend (returnValue);
                            }
                        }
                    }
                } catch (System.Reflection.TargetInvocationException ex) {
                    throw(ex.InnerException);
                }
            } else {
                throw (new ArgumentException ("method is not existing"));
            }
        }
开发者ID:xxtbg,项目名称:coredroidservice,代码行数:43,代码来源:SocketService.cs

示例4: ServiceLooper

        private static void ServiceLooper(NetworkStream stream, object service)
        {
            bool stopService = false;

            stream.DataSend (new OperationResultMessage (true));

            while (!stopService) {
                if (stream.DataAvailable) {
                    try {
                        switch (stream.DataReceive<ServiceRequestMessage> ().Action) {
                        case ServiceRequestAction.Call:
                            ServiceHandleCall (stream, service);
                            break;
                        case ServiceRequestAction.Close:
                            stopService = true;
                            break;
                        }
                    } catch (Exception ex) {
                        try {
                            do {
                                Thread.Sleep (200);
                                stream.Flush ();
                            } while (stream.DataAvailable);

                            stream.DataSend (new OperationResultMessage (ex));
                        } catch {
                            stopService = true;
                        }
                    }
                } else {
                    Thread.Sleep (200);
                }
            }
        }
开发者ID:xxtbg,项目名称:coredroidservice,代码行数:34,代码来源:SocketService.cs

示例5: LoadMono

        private static void LoadMono(NetworkStream stream)
        {
            try {
                using (MemoryStream ms = new MemoryStream()) {
                    SendStream (stream, ms);
                    stream.DataReceive<SendingPluginFinishedMessage> ();

                    AppDomain.CurrentDomain.Load (ms.ToArray ());

                    stream.DataSend (new OperationResultMessage (true));
                }
            } catch (Exception ex) {
                stream.DataSend (new OperationResultMessage (ex));
            }
        }
开发者ID:xxtbg,项目名称:coredroidservice,代码行数:15,代码来源:SocketService.cs


注:本文中的NetworkStream.DataReceive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。